%%======================================================================
%% Make hyphenation program from ukhypen.tex
%%----------------------------------------------------------------------
%% Copyright (C) 2003 Joe Armstrong
%%
%%   General Terms
%%
%%   Erlguten  is   free  software.   It   can  be  used,   modified  and
%% redistributed  by anybody for  personal or  commercial use.   The only
%% restriction  is  altering the  copyright  notice  associated with  the
%% material. Individuals or corporations are permitted to use, include or
%% modify the Erlguten engine.   All material developed with the Erlguten
%% language belongs to their respective copyright holder.
%% 
%%   Copyright Notice
%% 
%%   This  program is  free  software.  It  can  be redistributed  and/or
%% modified,  provided that this  copyright notice  is kept  intact. This
%% program is distributed in the hope that it will be useful, but without
%% any warranty; without even  the implied warranty of merchantability or
%% fitness for  a particular  purpose.  In no  event shall  the copyright
%% holder  be liable  for  any direct,  indirect,  incidental or  special
%% damages arising in any way out of the use of this software.
%%
%% Authors:   Joe Armstrong <joe@sics.se>
%% Last Edit: 2003-03-11
%% =====================================================================

-module(eg_mk_hyphen).

-export([start/0]).

-import(lists, [map/2, reverse/1, foreach/2]).

start() ->
    {ok, Bin} = file:read_file("../priv/ukhyphen.tex"),
    L = binary_to_list(Bin),
    Toks = string:tokens(L, "\n\r"),
    L1 = split1(Toks),
    {L2,L3} = split2(L1, []),
    {ok, O} = file:open("../src/eg_hyphen_rules.erl", [write]),
    io:format(O, "-module(eg_hyphen_rules).~n", []),
    io:format(O, "%% autogenerated do not edit~n",[]),
    io:format(O, "%%[{Char,Val}] = after char Char Val is inserted~n",[]),
    io:format(O, "-export([hyphens/1, exception/1]).~n~n",[]),
    Except = map(fun(I) -> clean(I) end, L3),
    foreach(fun(I) -> mk_exception(O, I) end, Except),
    io:format(O, "exception(_) -> no.~n~n", []),
    foreach(fun(I) -> jiggle(O, I) end, L2),
    io:format(O, "hyphens(_) -> [].~n",[]),
    file:close(O).

mk_exception(Stream, H) ->
    X = remove_hyphens(H),
    io:format(Stream, "exception(\"~s\") -> \"~s\";~n",[X, H]).

remove_hyphens([$-|T]) -> remove_hyphens(T);
remove_hyphens([H|T])  -> [H|remove_hyphens(T)];
remove_hyphens([])     -> [].

jiggle(O, X) ->
    {L1, L2} = jiggle(X, 0, [], []),
    %% io:format("~s~n",[X]),
    io:format(O, "hyphens(\"~s\" ++ _)->~p;~n",[L1,L2]).

jiggle([], _, L1, L2) ->
    {reverse(L1), L2};
jiggle([H|T], N, L1, L2) when H >= $1, H =< $9 ->
    jiggle(T, N, L1, [{N,H-$0}|L2]);
jiggle([$0|T], N, L1, L2) ->
    io:format("****?~n"),
    jiggle(T, N, L1, L2);
jiggle([H|T], N, L1, L2) ->
    jiggle(T, N+1, [H|L1], L2).
    

split1(["\\patterns" ++ _|T]) -> T;
split1([_|T])                 -> split1(T).

split2(["}" ++ _,"\\hyphenation"++_|T], L) -> {L, T};
split2([H|T], L)                           -> split2(T, [H|L]).

clean("}" ++ _) -> [];
clean([H|T])    -> [H|clean(T)];
clean([])       -> [].

    

